using System;
using System.Data;
using System.Data.SqlTypes;
using System.Data.SqlClient;
using System.Collections.Generic;

namespace Decision.BusinessLayer.DataLayer
{
	/// <summary>
	/// Data access layer class for QueryMasTerData
	/// </summary>
	class QueryMasTerDataSql : DataLayerBase 
	{

        #region Constructor

		/// <summary>
		/// Class constructor
		/// </summary>
		public QueryMasTerDataSql()
		{
			// Nothing for now.
		}

        #endregion

        #region Public Methods

        /// <summary>
        /// Select by Criteria
        /// </summary>
        /// <param name="Criteria">Criteria</param>
        /// <returns>QueryMasTerData business object</returns>
        public List<QueryMasTerData> SelectByObject(QueryMasTerDataCriteria value)
        {
            SqlCommand sqlCommand = new SqlCommand();
            sqlCommand.CommandText = "QueryMasTerData";
            sqlCommand.CommandType = CommandType.StoredProcedure;

            // Use connection object of base class
            sqlCommand.Connection = MainConnection;

            try
            {

			if (value.START_VOTE_DATE.HasValue)
			{
				sqlCommand.Parameters.Add(new SqlParameter("@START_VOTE_DATE", value.START_VOTE_DATE));
			}
			if (value.END_VOTE_DATE.HasValue)
			{
				sqlCommand.Parameters.Add(new SqlParameter("@END_VOTE_DATE", value.END_VOTE_DATE));
			}
			if (value.START_RESULT_DATE.HasValue)
			{
				sqlCommand.Parameters.Add(new SqlParameter("@START_RESULT_DATE", value.START_RESULT_DATE));
			}
			if (value.END_RESULT_DATE.HasValue)
			{
				sqlCommand.Parameters.Add(new SqlParameter("@END_RESULT_DATE", value.END_RESULT_DATE));
			}
			if (value.START_SIGN_DATE.HasValue)
			{
				sqlCommand.Parameters.Add(new SqlParameter("@START_SIGN_DATE", value.START_SIGN_DATE));
			}
			if (value.END_SIGN_DATE.HasValue)
			{
				sqlCommand.Parameters.Add(new SqlParameter("@END_SIGN_DATE", value.END_SIGN_DATE));
			}
			if (value.START_DECISION_DATE.HasValue)
			{
				sqlCommand.Parameters.Add(new SqlParameter("@START_DECISION_DATE", value.START_DECISION_DATE));
			}
			if (value.END_DECISION_DATE.HasValue)
			{
				sqlCommand.Parameters.Add(new SqlParameter("@END_DECISION_DATE", value.END_DECISION_DATE));
			}
			if (value.PROVINCE_ID > 0)
			{
				sqlCommand.Parameters.Add(new SqlParameter("@PROVINCE_ID", value.PROVINCE_ID));
			}
			if (value.MASTER_ID > 0)
			{
				sqlCommand.Parameters.Add(new SqlParameter("@MASTER_ID", value.MASTER_ID));
			}
			if (value.PageNum > 0)
			{
				sqlCommand.Parameters.Add(new SqlParameter("@PageNum", value.PageNum));
			}
			if (value.PageSize > 0)
			{
				sqlCommand.Parameters.Add(new SqlParameter("@PageSize", value.PageSize));
			}
			if (!string.IsNullOrEmpty(value.SortName))
			{
				sqlCommand.Parameters.Add(new SqlParameter("@SortName", value.SortName));
			}
			if (!string.IsNullOrEmpty(value.SortDirection))
			{
				sqlCommand.Parameters.Add(new SqlParameter("@SortDirection", value.SortDirection));
			}
			if (!string.IsNullOrEmpty(value.Field))
			{
				sqlCommand.Parameters.Add(new SqlParameter("@Field", value.Field));
			}
			if (!string.IsNullOrEmpty(value.Value))
			{
				sqlCommand.Parameters.Add(new SqlParameter("@Value", value.Value));
			}
			if (!string.IsNullOrEmpty(value.TERM_NO))
			{
				sqlCommand.Parameters.Add(new SqlParameter("@TERM_NO", value.TERM_NO));
			}
			if (!string.IsNullOrEmpty(value.NOTICE))
			{
				sqlCommand.Parameters.Add(new SqlParameter("@NOTICE", value.NOTICE));
			}
			if (!string.IsNullOrEmpty(value.VOTE_TYPE_ID))
			{
				sqlCommand.Parameters.Add(new SqlParameter("@VOTE_TYPE_ID", value.VOTE_TYPE_ID));
			}
            if (!string.IsNullOrEmpty(value.StatusID))
            {
                sqlCommand.Parameters.Add(new SqlParameter("@StatusID", value.StatusID));
            }
            if (value.CreatedUser != null)
            {
                sqlCommand.Parameters.Add(new SqlParameter("@CREATE_BY", value.CreatedUser));
            }

                MainConnection.Open();

                IDataReader dataReader = sqlCommand.ExecuteReader();


                return PopulateObjectsFromReader(dataReader);

            }
            catch (Exception ex)
            {
                throw new Exception("QueryMasTerData::SelectByObject::Error occured.", ex);
            }
            finally
            {             
                MainConnection.Close();
                sqlCommand.Dispose();
            }

        }

        #endregion

        #region Private Methods

        /// <summary>
        /// Populate business object from data reader
        /// </summary>
        /// <param name="businessObject">business object</param>
        /// <param name="dataReader">data reader</param>
        internal void PopulateBusinessObjectFromReader(QueryMasTerData businessObject, IDataReader dataReader)
        {


				if (!dataReader.IsDBNull(dataReader.GetOrdinal(QueryMasTerData.QueryMasTerDataFields.RowAll.ToString())))
				{
					businessObject.RowAll = dataReader.GetInt32(dataReader.GetOrdinal(QueryMasTerData.QueryMasTerDataFields.RowAll.ToString()));
				}

				if (!dataReader.IsDBNull(dataReader.GetOrdinal(QueryMasTerData.QueryMasTerDataFields.Row_No.ToString())))
				{
					businessObject.Row_No = dataReader.GetInt64(dataReader.GetOrdinal(QueryMasTerData.QueryMasTerDataFields.Row_No.ToString()));
				}

				if (!dataReader.IsDBNull(dataReader.GetOrdinal(QueryMasTerData.QueryMasTerDataFields.LAW_FNAME.ToString())))
				{
					businessObject.LAW_FNAME = dataReader.GetString(dataReader.GetOrdinal(QueryMasTerData.QueryMasTerDataFields.LAW_FNAME.ToString()));
				}

				if (!dataReader.IsDBNull(dataReader.GetOrdinal(QueryMasTerData.QueryMasTerDataFields.LNAME.ToString())))
				{
					businessObject.LNAME = dataReader.GetString(dataReader.GetOrdinal(QueryMasTerData.QueryMasTerDataFields.LNAME.ToString()));
				}

				businessObject.MASTER_ID = dataReader.GetInt32(dataReader.GetOrdinal(QueryMasTerData.QueryMasTerDataFields.MASTER_ID.ToString()));

				if (!dataReader.IsDBNull(dataReader.GetOrdinal(QueryMasTerData.QueryMasTerDataFields.CASE_TYPE_ID.ToString())))
				{
					businessObject.CASE_TYPE_ID = dataReader.GetInt32(dataReader.GetOrdinal(QueryMasTerData.QueryMasTerDataFields.CASE_TYPE_ID.ToString()));
				}

				if (!dataReader.IsDBNull(dataReader.GetOrdinal(QueryMasTerData.QueryMasTerDataFields.CASE_TYPE_NAME.ToString())))
				{
					businessObject.CASE_TYPE_NAME = dataReader.GetString(dataReader.GetOrdinal(QueryMasTerData.QueryMasTerDataFields.CASE_TYPE_NAME.ToString()));
				}

				if (!dataReader.IsDBNull(dataReader.GetOrdinal(QueryMasTerData.QueryMasTerDataFields.NOTICE.ToString())))
				{
					businessObject.NOTICE = dataReader.GetInt32(dataReader.GetOrdinal(QueryMasTerData.QueryMasTerDataFields.NOTICE.ToString()));
				}

				businessObject.NOTICE_NAME = dataReader.GetString(dataReader.GetOrdinal(QueryMasTerData.QueryMasTerDataFields.NOTICE_NAME.ToString()));

				if (!dataReader.IsDBNull(dataReader.GetOrdinal(QueryMasTerData.QueryMasTerDataFields.CODE_NO.ToString())))
				{
					businessObject.CODE_NO = dataReader.GetString(dataReader.GetOrdinal(QueryMasTerData.QueryMasTerDataFields.CODE_NO.ToString()));
				}

				if (!dataReader.IsDBNull(dataReader.GetOrdinal(QueryMasTerData.QueryMasTerDataFields.CREATE_DECISION_DATE.ToString())))
				{
					businessObject.CREATE_DECISION_DATE = dataReader.GetDateTime(dataReader.GetOrdinal(QueryMasTerData.QueryMasTerDataFields.CREATE_DECISION_DATE.ToString()));
				}

				if (!dataReader.IsDBNull(dataReader.GetOrdinal(QueryMasTerData.QueryMasTerDataFields.VOTE_TYPE_ID.ToString())))
				{
					businessObject.VOTE_TYPE_ID = dataReader.GetInt32(dataReader.GetOrdinal(QueryMasTerData.QueryMasTerDataFields.VOTE_TYPE_ID.ToString()));
				}

				if (!dataReader.IsDBNull(dataReader.GetOrdinal(QueryMasTerData.QueryMasTerDataFields.VOTE_NAME.ToString())))
				{
					businessObject.VOTE_NAME = dataReader.GetString(dataReader.GetOrdinal(QueryMasTerData.QueryMasTerDataFields.VOTE_NAME.ToString()));
				}

				if (!dataReader.IsDBNull(dataReader.GetOrdinal(QueryMasTerData.QueryMasTerDataFields.VOTE_CASE.ToString())))
				{
					businessObject.VOTE_CASE = dataReader.GetInt32(dataReader.GetOrdinal(QueryMasTerData.QueryMasTerDataFields.VOTE_CASE.ToString()));
				}

				businessObject.VOTE_CASE_NAME = dataReader.GetString(dataReader.GetOrdinal(QueryMasTerData.QueryMasTerDataFields.VOTE_CASE_NAME.ToString()));

				if (!dataReader.IsDBNull(dataReader.GetOrdinal(QueryMasTerData.QueryMasTerDataFields.COUNTY_NO.ToString())))
				{
					businessObject.COUNTY_NO = dataReader.GetString(dataReader.GetOrdinal(QueryMasTerData.QueryMasTerDataFields.COUNTY_NO.ToString()));
				}

				if (!dataReader.IsDBNull(dataReader.GetOrdinal(QueryMasTerData.QueryMasTerDataFields.COUNTY.ToString())))
				{
					businessObject.COUNTY = dataReader.GetString(dataReader.GetOrdinal(QueryMasTerData.QueryMasTerDataFields.COUNTY.ToString()));
				}

				if (!dataReader.IsDBNull(dataReader.GetOrdinal(QueryMasTerData.QueryMasTerDataFields.AUMPHER.ToString())))
				{
					businessObject.AUMPHER = dataReader.GetString(dataReader.GetOrdinal(QueryMasTerData.QueryMasTerDataFields.AUMPHER.ToString()));
				}

				if (!dataReader.IsDBNull(dataReader.GetOrdinal(QueryMasTerData.QueryMasTerDataFields.TUMBON.ToString())))
				{
					businessObject.TUMBON = dataReader.GetString(dataReader.GetOrdinal(QueryMasTerData.QueryMasTerDataFields.TUMBON.ToString()));
				}

				if (!dataReader.IsDBNull(dataReader.GetOrdinal(QueryMasTerData.QueryMasTerDataFields.PROVINCE.ToString())))
				{
					businessObject.PROVINCE = dataReader.GetString(dataReader.GetOrdinal(QueryMasTerData.QueryMasTerDataFields.PROVINCE.ToString()));
				}

				if (!dataReader.IsDBNull(dataReader.GetOrdinal(QueryMasTerData.QueryMasTerDataFields.PROVINCE_NAME.ToString())))
				{
					businessObject.PROVINCE_NAME = dataReader.GetString(dataReader.GetOrdinal(QueryMasTerData.QueryMasTerDataFields.PROVINCE_NAME.ToString()));
				}

				if (!dataReader.IsDBNull(dataReader.GetOrdinal(QueryMasTerData.QueryMasTerDataFields.TERM_NO.ToString())))
				{
					businessObject.TERM_NO = dataReader.GetString(dataReader.GetOrdinal(QueryMasTerData.QueryMasTerDataFields.TERM_NO.ToString()));
				}

				if (!dataReader.IsDBNull(dataReader.GetOrdinal(QueryMasTerData.QueryMasTerDataFields.DECISION_NO.ToString())))
				{
					businessObject.DECISION_NO = dataReader.GetString(dataReader.GetOrdinal(QueryMasTerData.QueryMasTerDataFields.DECISION_NO.ToString()));
				}

				if (!dataReader.IsDBNull(dataReader.GetOrdinal(QueryMasTerData.QueryMasTerDataFields.CASE_NO.ToString())))
				{
					businessObject.CASE_NO = dataReader.GetString(dataReader.GetOrdinal(QueryMasTerData.QueryMasTerDataFields.CASE_NO.ToString()));
				}

				if (!dataReader.IsDBNull(dataReader.GetOrdinal(QueryMasTerData.QueryMasTerDataFields.RESULT_NO.ToString())))
				{
					businessObject.RESULT_NO = dataReader.GetString(dataReader.GetOrdinal(QueryMasTerData.QueryMasTerDataFields.RESULT_NO.ToString()));
				}

				if (!dataReader.IsDBNull(dataReader.GetOrdinal(QueryMasTerData.QueryMasTerDataFields.RESULT_TYPE.ToString())))
				{
					businessObject.RESULT_TYPE = dataReader.GetInt32(dataReader.GetOrdinal(QueryMasTerData.QueryMasTerDataFields.RESULT_TYPE.ToString()));
				}

				businessObject.RESULT_TYPE_NAME = dataReader.GetString(dataReader.GetOrdinal(QueryMasTerData.QueryMasTerDataFields.RESULT_TYPE_NAME.ToString()));

				businessObject.COURT_TYPE_NAME = dataReader.GetString(dataReader.GetOrdinal(QueryMasTerData.QueryMasTerDataFields.COURT_TYPE_NAME.ToString()));

				if (!dataReader.IsDBNull(dataReader.GetOrdinal(QueryMasTerData.QueryMasTerDataFields.COURT_TYPE_ID.ToString())))
				{
					businessObject.COURT_TYPE_ID = dataReader.GetInt32(dataReader.GetOrdinal(QueryMasTerData.QueryMasTerDataFields.COURT_TYPE_ID.ToString()));
				}

				if (!dataReader.IsDBNull(dataReader.GetOrdinal(QueryMasTerData.QueryMasTerDataFields.COURT_A_NAME.ToString())))
				{
					businessObject.COURT_A_NAME = dataReader.GetString(dataReader.GetOrdinal(QueryMasTerData.QueryMasTerDataFields.COURT_A_NAME.ToString()));
				}

				if (!dataReader.IsDBNull(dataReader.GetOrdinal(QueryMasTerData.QueryMasTerDataFields.COURT_CONSTITU_TYPE.ToString())))
				{
					businessObject.COURT_CONSTITU_TYPE = dataReader.GetInt32(dataReader.GetOrdinal(QueryMasTerData.QueryMasTerDataFields.COURT_CONSTITU_TYPE.ToString()));
				}

				if (!dataReader.IsDBNull(dataReader.GetOrdinal(QueryMasTerData.QueryMasTerDataFields.COURT_NAME.ToString())))
				{
					businessObject.COURT_NAME = dataReader.GetString(dataReader.GetOrdinal(QueryMasTerData.QueryMasTerDataFields.COURT_NAME.ToString()));
				}

				if (!dataReader.IsDBNull(dataReader.GetOrdinal(QueryMasTerData.QueryMasTerDataFields.COURT_B_NAME.ToString())))
				{
					businessObject.COURT_B_NAME = dataReader.GetString(dataReader.GetOrdinal(QueryMasTerData.QueryMasTerDataFields.COURT_B_NAME.ToString()));
				}

				if (!dataReader.IsDBNull(dataReader.GetOrdinal(QueryMasTerData.QueryMasTerDataFields.COURT_B_SUPREME_TYPE_ID.ToString())))
				{
					businessObject.COURT_B_SUPREME_TYPE_ID = dataReader.GetInt32(dataReader.GetOrdinal(QueryMasTerData.QueryMasTerDataFields.COURT_B_SUPREME_TYPE_ID.ToString()));
				}

				if (!dataReader.IsDBNull(dataReader.GetOrdinal(QueryMasTerData.QueryMasTerDataFields.COURT_A_SUPREME_TYPE_ID.ToString())))
				{
					businessObject.COURT_A_SUPREME_TYPE_ID = dataReader.GetInt32(dataReader.GetOrdinal(QueryMasTerData.QueryMasTerDataFields.COURT_A_SUPREME_TYPE_ID.ToString()));
				}

				if (!dataReader.IsDBNull(dataReader.GetOrdinal(QueryMasTerData.QueryMasTerDataFields.SIGN_DATE.ToString())))
				{
					businessObject.SIGN_DATE = dataReader.GetDateTime(dataReader.GetOrdinal(QueryMasTerData.QueryMasTerDataFields.SIGN_DATE.ToString()));
				}

				if (!dataReader.IsDBNull(dataReader.GetOrdinal(QueryMasTerData.QueryMasTerDataFields.WARN_SIGN_DATE.ToString())))
				{
					businessObject.WARN_SIGN_DATE = dataReader.GetDateTime(dataReader.GetOrdinal(QueryMasTerData.QueryMasTerDataFields.WARN_SIGN_DATE.ToString()));
				}

				if (!dataReader.IsDBNull(dataReader.GetOrdinal(QueryMasTerData.QueryMasTerDataFields.VOTE_DATE.ToString())))
				{
					businessObject.VOTE_DATE = dataReader.GetDateTime(dataReader.GetOrdinal(QueryMasTerData.QueryMasTerDataFields.VOTE_DATE.ToString()));
				}

				if (!dataReader.IsDBNull(dataReader.GetOrdinal(QueryMasTerData.QueryMasTerDataFields.RESULT_DATE.ToString())))
				{
					businessObject.RESULT_DATE = dataReader.GetDateTime(dataReader.GetOrdinal(QueryMasTerData.QueryMasTerDataFields.RESULT_DATE.ToString()));
				}

				if (!dataReader.IsDBNull(dataReader.GetOrdinal(QueryMasTerData.QueryMasTerDataFields.WARN_RESULT_DATE.ToString())))
				{
					businessObject.WARN_RESULT_DATE = dataReader.GetDateTime(dataReader.GetOrdinal(QueryMasTerData.QueryMasTerDataFields.WARN_RESULT_DATE.ToString()));
				}

				if (!dataReader.IsDBNull(dataReader.GetOrdinal(QueryMasTerData.QueryMasTerDataFields.REQUEST_NAME.ToString())))
				{
					businessObject.REQUEST_NAME = dataReader.GetString(dataReader.GetOrdinal(QueryMasTerData.QueryMasTerDataFields.REQUEST_NAME.ToString()));
				}

				if (!dataReader.IsDBNull(dataReader.GetOrdinal(QueryMasTerData.QueryMasTerDataFields.RESPON_NAME.ToString())))
				{
					businessObject.RESPON_NAME = dataReader.GetString(dataReader.GetOrdinal(QueryMasTerData.QueryMasTerDataFields.RESPON_NAME.ToString()));
				}

				if (!dataReader.IsDBNull(dataReader.GetOrdinal(QueryMasTerData.QueryMasTerDataFields.ALLEGATION.ToString())))
				{
					businessObject.ALLEGATION = dataReader.GetString(dataReader.GetOrdinal(QueryMasTerData.QueryMasTerDataFields.ALLEGATION.ToString()));
				}

				if (!dataReader.IsDBNull(dataReader.GetOrdinal(QueryMasTerData.QueryMasTerDataFields.LAWYER_ID.ToString())))
				{
					businessObject.LAWYER_ID = dataReader.GetInt32(dataReader.GetOrdinal(QueryMasTerData.QueryMasTerDataFields.LAWYER_ID.ToString()));
				}

				if (!dataReader.IsDBNull(dataReader.GetOrdinal(QueryMasTerData.QueryMasTerDataFields.AMPHUR_NAME.ToString())))
				{
					businessObject.AMPHUR_NAME = dataReader.GetString(dataReader.GetOrdinal(QueryMasTerData.QueryMasTerDataFields.AMPHUR_NAME.ToString()));
				}

				if (!dataReader.IsDBNull(dataReader.GetOrdinal(QueryMasTerData.QueryMasTerDataFields.DISTRICT_NAME.ToString())))
				{
					businessObject.DISTRICT_NAME = dataReader.GetString(dataReader.GetOrdinal(QueryMasTerData.QueryMasTerDataFields.DISTRICT_NAME.ToString()));
				}

				if (!dataReader.IsDBNull(dataReader.GetOrdinal(QueryMasTerData.QueryMasTerDataFields.STATUS.ToString())))
				{
					businessObject.STATUS = dataReader.GetInt32(dataReader.GetOrdinal(QueryMasTerData.QueryMasTerDataFields.STATUS.ToString()));
				}

				if (!dataReader.IsDBNull(dataReader.GetOrdinal(QueryMasTerData.QueryMasTerDataFields.CREATED_DATE_TIME.ToString())))
				{
					businessObject.CREATED_DATE_TIME = dataReader.GetDateTime(dataReader.GetOrdinal(QueryMasTerData.QueryMasTerDataFields.CREATED_DATE_TIME.ToString()));
				}

				if (!dataReader.IsDBNull(dataReader.GetOrdinal(QueryMasTerData.QueryMasTerDataFields.CREATED_USER.ToString())))
				{
					businessObject.CREATED_USER = dataReader.GetInt32(dataReader.GetOrdinal(QueryMasTerData.QueryMasTerDataFields.CREATED_USER.ToString()));
				}

				if (!dataReader.IsDBNull(dataReader.GetOrdinal(QueryMasTerData.QueryMasTerDataFields.UPDATED_DATE_TIME.ToString())))
				{
					businessObject.UPDATED_DATE_TIME = dataReader.GetDateTime(dataReader.GetOrdinal(QueryMasTerData.QueryMasTerDataFields.UPDATED_DATE_TIME.ToString()));
				}

				if (!dataReader.IsDBNull(dataReader.GetOrdinal(QueryMasTerData.QueryMasTerDataFields.UPDATED_USER.ToString())))
				{
					businessObject.UPDATED_USER = dataReader.GetInt32(dataReader.GetOrdinal(QueryMasTerData.QueryMasTerDataFields.UPDATED_USER.ToString()));
				}

                if (!dataReader.IsDBNull(dataReader.GetOrdinal(QueryMasTerData.QueryMasTerDataFields.REMARK.ToString())))
                {
                    businessObject.REMARK = dataReader.GetString(dataReader.GetOrdinal(DCS_MASTER.DCS_MASTERFields.REMARK.ToString()));
                }

        }

        /// <summary>
        /// Populate business objects from the data reader
        /// </summary>
        /// <param name="dataReader">data reader</param>
        /// <returns>list of QueryMasTerData</returns>
        internal List<QueryMasTerData> PopulateObjectsFromReader(IDataReader dataReader)
        {

            List<QueryMasTerData> list = new List<QueryMasTerData>();

            while (dataReader.Read())
            {
                QueryMasTerData businessObject = new QueryMasTerData();
                PopulateBusinessObjectFromReader(businessObject, dataReader);
                list.Add(businessObject);
            }
            return list;

        }

        #endregion

	}
}
